home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreappleevents / osahelpers.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  7.6 KB  |  282 lines

  1. /*
  2.     File:        OSAHelpers.c
  3.  
  4.     Contains:    Functions to help you when working with the OSA.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 12/16/98    afb                Fixed leak of AEDesc in OSAHUnloadScriptResource.
  21.                                              Changed call in OSAHUnloadScriptResource to use file 
  22.                                              reference passed in, rather than CurResFile().
  23.  
  24. */
  25.  
  26.  
  27. //    Conditionals to setup the build environment the way we like it.
  28. #include "PrivateConditionals.h"
  29.                                                                             
  30.  
  31. //**************    Universal Headers        ********************************
  32.  
  33. #include <AppleScript.h>
  34. #include <ASDebugging.h>
  35. #include <OSA.h>
  36. #include <OSAGeneric.h>
  37. #include <Resources.h>
  38.  
  39.  
  40. //**************    ANSI Headers            ********************************
  41.  
  42. #include <string.h>
  43.  
  44.  
  45. //**************    Project Headers            ********************************
  46.  
  47. #include "AEHelpers.h"
  48. #include "OSAHelpers.h"
  49.  
  50.  
  51. //**********    Private Definitions        ****************************************
  52.  
  53. #define    kFlagNotSet    -1
  54.  
  55.  
  56. //*************************************************************************
  57.  
  58. pascal Boolean OSAHAppleScriptIsPresent( void )
  59. {
  60.     OSErr    anErr = noErr;
  61.     
  62.     static    long        gHasAppleScript = kFlagNotSet;
  63.     
  64.     if ( gHasAppleScript == kFlagNotSet )
  65.     {
  66.         if ( GestaltAvailable() )
  67.         {
  68.             long    response;
  69.             
  70.             if ( Gestalt( gestaltAppleScriptAttr, &response ) == noErr )
  71.             {
  72.                 gHasAppleScript = ( response & (1L << gestaltAppleScriptPresent) ) != 0;
  73.             }
  74.         }
  75.         else
  76.         {
  77.             gHasAppleScript = false;
  78.         }
  79.     }
  80.     
  81.     return gHasAppleScript;
  82. }//end HasAppleEvents
  83.  
  84. //*************************************************************************
  85.  
  86. pascal OSErr OSAHOpenGenericScriptingComponent( ComponentInstance *compInstPtr )
  87. {
  88.     OSErr    anErr = noErr;
  89.     
  90.     if ( HasAppleEvents() )
  91.     {
  92.         *compInstPtr = OpenDefaultComponent( kOSAComponentType, kOSAGenericScriptingComponentSubtype );
  93.         if ( (*compInstPtr == (ComponentInstance)badComponentInstance)
  94.           || (*compInstPtr == (ComponentInstance)badComponentSelector))
  95.         {
  96.             *compInstPtr = 0;                    //    Set to 0 if no component is found.
  97.             anErr = errOSACantOpenComponent;
  98.         }
  99.     }
  100.     else
  101.     {
  102.         anErr = errOSACantOpenComponent;
  103.     }
  104.     
  105.     return ( anErr );
  106. }//end OSAHOpenGenericScriptingComponent
  107.  
  108. //*************************************************************************
  109.  
  110. pascal OSErr OSAHOpenAppleScriptComponent( ComponentInstance *compInstPtr )
  111. {
  112.     OSErr    anErr = noErr;
  113.     
  114.     if ( HasAppleEvents() )
  115.     {
  116.         *compInstPtr = OpenDefaultComponent( kOSAComponentType, kAppleScriptSubtype );
  117.         if ( (*compInstPtr == (ComponentInstance)badComponentInstance)
  118.           || (*compInstPtr == (ComponentInstance)badComponentSelector))
  119.         {
  120.             *compInstPtr = 0;                    //    Set to 0 if no component is found.
  121.             anErr = errOSACantOpenComponent;
  122.         }
  123.     }
  124.     else
  125.     {
  126.         anErr = errOSACantOpenComponent;
  127.     }
  128.     
  129.     return ( anErr );
  130. }//end OSAHOpenAppleScriptingComponent
  131.  
  132. //*************************************************************************
  133.  
  134. pascal OSErr OSAHLoadScriptResource( ComponentInstance componentInstance, short resFileRef, short scriptResID, OSAID *scriptIDPtr )
  135. {
  136.     Handle        scriptHnd = nil;
  137.     OSAError    anErr = noErr;
  138.     short        originalResFile = CurResFile();
  139.     
  140.     UseResFile( resFileRef );
  141.     
  142.     *scriptIDPtr = kOSANullScript;        //    Set to null incase it can't be loaded
  143.     
  144.     scriptHnd = Get1Resource( typeScript, scriptResID );
  145.     if ( scriptHnd == nil )
  146.     {
  147.         anErr = resNotFound;
  148.     }
  149.     else
  150.     {
  151.         AEDesc    scriptDesc;
  152.         
  153.         scriptDesc.descriptorType = typeOSAGenericStorage;
  154.         scriptDesc.dataHandle = scriptHnd;
  155.         anErr = OSALoad( componentInstance, &scriptDesc, kOSAModeNull, scriptIDPtr );
  156.         ReleaseResource( scriptHnd );    //    Once loaded, don't need the script resource any more.
  157.     }
  158.     
  159.     UseResFile( originalResFile );
  160.     
  161.     return anErr;
  162. }//end OSAHLoadScriptResource
  163.  
  164. //*************************************************************************
  165.  
  166. pascal OSErr OSAHUnloadScriptResource( ComponentInstance componentInstance, short resFileRef, short scriptResID, OSAID scriptID )
  167. {
  168.     OSAError    osaErr = noErr;
  169.     
  170.     long        scriptIsModified = 0;    
  171.     short        originalResFile = CurResFile();
  172.     
  173.     UseResFile( resFileRef );
  174.     osaErr = OSAGetScriptInfo( componentInstance, scriptID, kOSAScriptIsModified, &scriptIsModified );
  175.     if ( osaErr == noErr  &&  scriptIsModified > 0 )
  176.     {
  177.         Handle    scriptHnd = Get1Resource( typeScript, scriptResID );
  178.         if ( scriptHnd == nil )
  179.         {
  180.             osaErr = resNotFound;
  181.         }
  182.         else
  183.         {
  184.             AEDesc    scriptDesc = { typeNull, nil };
  185.             
  186.             osaErr = OSAStore( componentInstance, scriptID, typeOSAGenericStorage, kOSAModeNull, &scriptDesc );
  187.             if ( osaErr == noErr )
  188.             {
  189.                 Size    scriptSize = GetHandleSize( scriptDesc.dataHandle );
  190.                 SetHandleSize( scriptHnd, scriptSize );
  191.                 osaErr = MemError();
  192.                 if ( osaErr == noErr )
  193.                 {
  194.                     BlockMoveData( *scriptDesc.dataHandle, *scriptHnd, scriptSize );
  195.                     ChangedResource( scriptHnd );
  196.                     osaErr = ResError();
  197.                     if ( osaErr == noErr )
  198.                     {
  199.                         UpdateResFile( resFileRef );
  200.                         osaErr = ResError();
  201.                     }
  202.                 }
  203.                 (void) AEDisposeDesc( &scriptDesc );
  204.             }
  205.             ReleaseResource( scriptHnd );
  206.         }
  207.     }
  208.     UseResFile( originalResFile );
  209.  
  210.     return osaErr;
  211. }//end OSAHUnloadScriptResource
  212.  
  213. //*************************************************************************
  214.  
  215. pascal OSAError OSAHGetHandlerNames( ComponentInstance compInst, OSAID scriptID, AEDescList* handlerListPtr )
  216. {
  217.     OSAError            anErr = noErr;
  218.     ComponentInstance    asCompInst;
  219.     
  220.     anErr = OSAGenericToRealID( compInst, &scriptID, &asCompInst );
  221.     if ( anErr == noErr )
  222.     {
  223.         anErr = OSAGetHandlerNames( asCompInst, kOSANullMode, scriptID, handlerListPtr );
  224.     }
  225.     return anErr;
  226. }//end OSAHGetHandlerNames
  227.  
  228. //*************************************************************************
  229.  
  230. pascal Boolean OSAHHandlerIsInHandlerList( const AEDescList *handlerListPtr, const AEDesc *nameDescPtr )
  231. {
  232.     OSErr        anErr = noErr;
  233.     Boolean        hasHandler = false;
  234.     
  235.     SInt32    itemCount;
  236.         
  237.     anErr = AECountItems( handlerListPtr, &itemCount );
  238.     if ( anErr == noErr )
  239.     {
  240.         UInt32        index;
  241.         DescType    nameType = nameDescPtr->descriptorType;
  242.         
  243.         for ( index = 1; index <= itemCount; index++ )
  244.         {
  245.             SInt32        actualSize;
  246.             char        handlerName[128];
  247.             DescType    keyWord;
  248.             DescType    actualType;        
  249.             
  250.             anErr = AEGetNthPtr( handlerListPtr, index, nameType, &keyWord, &actualType, 
  251.                                  &handlerName, sizeof( handlerName ), &actualSize );
  252.             if ( anErr == noErr  &&  strncmp( handlerName, (char*)&(*nameDescPtr->dataHandle), actualSize ) == 0 )
  253.             {
  254.                 hasHandler = true;
  255.                 break;
  256.             }
  257.         }
  258.     }
  259.     return hasHandler;
  260. }//end OSAHHandlerIsInHandlerList
  261.  
  262. //*************************************************************************
  263.  
  264. pascal Boolean OSAHScriptHasHandler( ComponentInstance compInst, OSAID scriptID, const AEDesc *nameDescPtr )
  265. {
  266.     AEDescList    handlerList;
  267.     OSErr        anErr = noErr;
  268.     Boolean        hasHandler = false;
  269.     
  270.     anErr = OSAHGetHandlerNames( compInst, scriptID, &handlerList );
  271.     if ( anErr == noErr )
  272.     {
  273.         hasHandler = OSAHHandlerIsInHandlerList( &handlerList, nameDescPtr );
  274.     }
  275.     AEDisposeDesc( &handlerList );
  276.     
  277.     return hasHandler;
  278. }//end OSAHScriptHasHandler
  279.  
  280. //*************************************************************************
  281.  
  282.